library(logr)
log_open("log_A1.log")
log_code()
####################
#REPLICATION FILES: APPENDIX 1
#Article: "When Does Online Public Diplomacy Succeed? Evidence from China's ‘Wolf Warrior’ Diplomats"
#Authors: Daniel Mattingly and James Sundquist
#This Version: June 18, 2022


####################
#Description: Running this script will create the tables presented in Appendix 1 of the article.
#The script was written with R version 4.1.2
#It was created and tested on Mac OS X (11.6.5).
####################


# Load required packages
library(tidyverse)
library(xtable)
library(rio)
library(coefplot)

# Set working directory
setwd("~/Dropbox/Research/Diplomacy/Replication")

#Load data
pooled <- import("twitter_diplomacy_data.csv")
# Ensure levels of treatment variable appear in proper order
pooled$Treatment <- factor(pooled$Treatment, levels = c("control", "prochina", "antius"))
pooled$t_ <- pooled$Treatment


###
# A.1 Descriptive Statistics
##

# Renaming and coarsening covariates
pooled <- pooled %>% dplyr::rename(Female = gender, Age = age_category, conservative = left_right,
                            College = education, knowledgable = china_knowledge)
pooled$Female <- if_else(pooled$Female == "Female", 1, 0)
pooled$Under40 <- recode(pooled$Age, `18-24` = 1, `25-29` = 1, `30-39` = 1, `40-49` = 0,
                     `50-59` = 0, `60-69` = 0, `70 or older` = 0)
pooled$College <- if_else(pooled$College == "Graduated from college" | pooled$College == "Obtained a professional degree beyond a college degree",
                          1, 0)
pooled$stance <- recode(pooled$hawk_dove, `Ally` = "dove", `Partner` = "dove", `None of these words are a good fit` = "neither",
                     `Rival` = "hawk", `Enemy` = "hawk")
pooled$Hawk <- if_else(pooled$stance == "hawk", 1, 0)
pooled$Dove <- if_else(pooled$stance == "dove", 1, 0)
pooled <- pooled %>% mutate(knowledgable = if_else(knowledgable == "Hu Jintao", 1, 0))
pooled$Extreme_left <- if_else(pooled$conservative == 1, 1, 0)
pooled$Extreme_right <- if_else(pooled$conservative == 7, 1, 0)

myvars <- pooled %>% select(Under40, Female, College, Dove, Hawk, Extreme_left, Extreme_right)
myvars <- myvars %>% pivot_longer(cols = everything(), names_to = "Statistic")

# Create columns of table
my_n <- myvars %>% group_by(Statistic) %>% summarise(N = sum(!is.na(value)))
my_mean <-myvars %>% group_by(Statistic) %>% summarise(Mean = mean(value, na.rm = TRUE))
my_sd <-myvars %>% group_by(Statistic) %>% summarise(`St. Dev.` = sd(value, na.rm = TRUE))
my_min <-myvars %>% group_by(Statistic) %>% summarise(Min = min(value, na.rm = TRUE))
my_max <-myvars %>% group_by(Statistic) %>% summarise(Max = max(value, na.rm = TRUE))

# Join columns together
summary_stats <- my_n %>% left_join(my_mean) %>%
                          left_join(my_sd) %>% 
                          left_join(my_min) %>% 
                          left_join(my_max)
# Produce table
a1 <- print(xtable(summary_stats, digits = 3))
write(a1, file = "TableA1.tex")

log_close()

